/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  DetectItemFragment.java   
 * @Package com.jiyi.activity.new_maintenance   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-8-30 下午2:45:48   
 * @version V1.0     
 */
package com.jiyi.activity.new_maintenance;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiyi.adapter.DetectItemReadAdapter;
import com.jiyi.bean.maintenance.DetectItemParent;
import com.jiyi.bean.maintenance.DetectSystemJson.Equipment;
import com.jiyi.bean.maintenance.DetectSystemJson.SecEquipment;
import com.swift.maintenance.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName: DetectItemFragment
 * @Description:TODO(检测系统,只读)
 * @author: HYM
 * @date: 2017-8-30 下午2:45:48
 * 
 */
public class DetectSystemReadFragment extends Fragment {

	LayoutInflater inflater;

	@BindView(R.id.mainList)
	ExpandableListView mainList;
	@BindView(R.id.topDetectTip)
	TextView topDetectTip;

	DetectItemReadAdapter mDetectItemReadAdapter;

	ArrayList<DetectItemParent> mDetectItemParents = new ArrayList<DetectItemParent>();
	ViewGroup container;

	SecEquipment secEquipment;
	// 第几个父节点
	int position;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_detectitem_read, null,
				false);
		ButterKnife.bind(this, view);
		this.inflater = inflater;
		this.container = container;
		init();
		return view;
	}

	private void init() {
		getIntentData();
		setAdapter();
		getDetectItemData();
	}

	/**
	 * @author: HYM
	 * @Title: getIntentData
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-8-31
	 */
	private void getIntentData() {
		position = getArguments().getInt("position");
		String secEquipment = getArguments().getString("secEquipment");
		this.secEquipment = new Gson().fromJson(secEquipment,
				SecEquipment.class);
	}

	/**
	 * @author: HYM
	 * @Title: getDetectItemData
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-8-30
	 */
	private void getDetectItemData() {
		for (int i = 0; i < secEquipment.getEquipment_list().size(); i++) {
			Equipment equipment = secEquipment.getEquipment_list().get(i);
			DetectItemParent parent = new DetectItemParent();
			parent.setTitle(equipment.getName());
			List<Equipment> childList = new ArrayList<Equipment>();
			Equipment child = equipment;
			childList.add(child);
			parent.setChildList(childList);
			mDetectItemParents.add(parent);
		}
		mDetectItemReadAdapter.notifyDataSetChanged();
		if (secEquipment.getIs_spotcheck() == 0) {
			topDetectTip.setVisibility(View.GONE);
		}
		else {
			topDetectTip.setVisibility(View.VISIBLE);
		}
	}

	private void setAdapter() {
		mDetectItemReadAdapter = new DetectItemReadAdapter(getContext(),
				position, mDetectItemParents);
		mainList.setGroupIndicator(null);// 这个是去掉父级的箭头
		mainList.setAdapter(mDetectItemReadAdapter);
		mainList.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				return true;
			}
		});

	}
}
